import {Cart} from 'cart-model.js'
var cart = new Cart()

// cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var cartData = cart.getCartData()
    var calculateInfo = this.getCalculateData(cartData);
    
    this.setData({
      "account": calculateInfo.account,
      "selectedCounts": calculateInfo.selectedCounts,
      "selectedTypeCounts": calculateInfo.selectedTypeCounts,
      "cartData":cartData
    });
  },

  /**
   * 点击复选框的时候
   * 
   */
  toggleSelect:function(event){
      var id = cart.getDataset(event, 'id');
      var selectedStatus = cart.getDataset(event, 'status');
      var cartData = this.data.cartData;

      var cartIndex = cart._hasThatOne(id, cartData);
      if(cartIndex.index == -1) return;

      cartData[cartIndex.index].selectedStatus = !selectedStatus;
      cart.setCartData(cartData);

      this._reCalCartData();
  },

  /**
   * 点击全选框时候的事件
   */
  toggleSelectAll: function(event) {
    var status = cart.getDataset(event, 'status');
    console.log(typeof status);

    var cartData = cart.getCartData();
    for(let i=0; i<cartData.length; i++) {
      cartData[i].selectedStatus = !status;
    }

    cart.setCartData(cartData);
    this._reCalCartData();
  },

  /**
   * 通过 + - 号 更改商品数量
   */
  changeCounts: function(event) {
    var id = cart.getDataset(event, 'id');
    var type = cart.getDataset(event, 'type');

    this._changeCartCounts(id, type);
  },

  _changeCartCounts: function(id, type) {
    var cartData = this.data.cartData;
    var cartIndex = cart._hasThatOne(id, cartData);
    if (cartIndex.index == -1) return;

    var cartCount = cartData[cartIndex.index].count;
    if (type == 'cut' && cartCount < 1) return;

    cartData[cartIndex.index].count = cartCount + (type == 'cut'? -1 : 1);
    
    cart.setCartData(cartData);
    this._reCalCartData();
  },

  delete: function(event) {
    var id = cart.getDataset(event, 'id');
    var cartData = this.data.cartData;
    var cartIndex = cart._hasThatOne(id, cartData);
    if (cartIndex.index == -1) return;

    cartData.splice(cartIndex.index, 1);
    cart.setCartData(cartData);
    this._reCalCartData();
  },

  /**
   * 重新计算购物车的数据
   */
  _reCalCartData:function(){
    var cartData = cart.getCartData();
    var calculateInfo = this.getCalculateData(cartData);
    
    this.setData({
      "account": calculateInfo.account,
      "selectedCounts": calculateInfo.selectedCounts,
      "selectedTypeCounts": calculateInfo.selectedTypeCounts,
      "cartData": cartData
    });
  },

  /**
   * 获取统计信息
   */
  getCalculateData: function(data) {
    var priceTotal = 0;
    var selectedProductCount = 0;
    var selectTypeCount = 0;
    var multiple = 100; 


    for(let i=0; i<data.length; i++) {
      if (data[i].selectedStatus) {
        selectedProductCount += data[i].count;
        selectTypeCount++;

        priceTotal += multiple * data[i].count * Number(data[i].price) * multiple;

      }
    }

    return {
      "account": priceTotal / multiple / multiple,
      "selectedCounts": selectedProductCount,
      "selectedTypeCounts": selectTypeCount
    }
  }
})